package nac.slick2dkryonet.server;
import java.util.ArrayList;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import nac.net.Network;
import nac.net.Network.Plane;

public class SlickServerMain {

    private Plane[] planes = {null, null, null, null, null, null, null, null, null, null};
    private Server server;

    public SlickServerMain() {
        server = new Server();
        Network.register(server);
        server.addListener(new ServerListener());
        try {
            server.bind(Network.PORT);

        } catch (IOException ex) {
            Logger.getLogger(SlickServerMain.class.getName()).log(Level.SEVERE, null, ex);
        }
        server.start();
        JFrame frame = new JFrame("Nathaniel Camomot");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosed(WindowEvent evt) {
                server.stop();
            }
        });
        frame.getContentPane().add(new JLabel("     Close to stop server."));
        frame.setSize(320, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SlickServerMain ssm = new SlickServerMain();

    }

    private class ServerListener extends Listener {

        @Override
        public void received(Connection connection, Object object) {
            planes[connection.getID()] = (Plane) object;
            server.sendToAllTCP(planes);
        }
    }
}
